/**
 * 
 */
package com.Dialogs;


import android.app.AlertDialog;
import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.TextFormat.UtilFonts;
import com.shareup.R;
import com.shareup.R.id;


/**
 * @author Chris
 * 
 */
public class DialogOneText extends AlertDialog {

    private int type = InputType.TYPE_CLASS_TEXT;

    private int MaxLength;

    private int MinLength;

    public String getHint() {

        return this.hint;
    }

    public int getType() {

        return this.type;
    }

    public void setType(int type) {

        this.type = type;
    }

    private String Text;

    public void setHint(String hint) {

        this.hint = hint;
    }

    public String getText() {

        return this.Text;
    }

    public void setText(String text) {

        this.Text = text;
    }

    public void setOnClick(OnClickOkListener onClick) {

        this.OnClick = onClick;
    }

    private OnClickOkListener OnClick;

    private String hint;

    public DialogOneText(Context context, OnClickOkListener OnClick) {

        super(context);
        this.context = context;
        this.OnClick = OnClick;

    }

    @Override
    public void show() {

        inflate();
        super.show();
    }

    private Context context;

    private void inflate() {

        View v = LayoutInflater.from(context).inflate(R.layout.dialog_one_text, null);

        final EditText text = (EditText) v.findViewById(id.edit1);

        if (Text != null && !Text.equals("") && !Text.equals("0"))
            text.setText(Text);

        text.setHint(hint);
        text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MaxLength) });
        text.setRawInputType(type);
        Button but = (Button) v.findViewById(R.id.button_OK);
        but.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String err = "";
                if (text.length() < MinLength) {
                    err = String.format(v.getResources().getString(R.string.error_pin_short), MinLength
                                + "");
                    text.setError(err);
                }
                else
                    if (type == InputType.TYPE_CLASS_NUMBER)
                        if (!isNumeric(text.getText().toString())) {
                            err = v.getResources().getString(R.string.error_numbers_only);
                            text.setError(err);
                        }
                        else
                            gotoCallback(text.getText().toString());
                    else
                        gotoCallback(text.getText().toString());

            }

        });

        UtilFonts.setTextRegularFont(text, but);
        this.setView(v);

    }

    private void gotoCallback(String text) {

        dismiss();

        if (OnClick != null)
            OnClick.OnClick(text);
    }

    private boolean isNumeric(String input) {

        try {
            Integer.parseInt(input);
            return true;

        }
        catch (Exception x) {
            return false;
        }
    }

    public int getMaxLength() {

        return this.MaxLength;
    }

    public void setMaxLength(int maxLength) {

        this.MaxLength = maxLength;
    }

    public int getMinLength() {

        return this.MinLength;
    }

    public void setMinLength(int minLength) {

        this.MinLength = minLength;
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    public interface OnClickOkListener {

        void OnClick(String text);

    }

}
